{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# bin_packing_mip"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/linear_solver/bin_packing_mip.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/ortools/linear_solver/samples/bin_packing_mip.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "Solve a simple bin packing problem using a MIP solver."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.linear_solver import pywraplp\n",
    "\n",
    "\n",
    "def create_data_model():\n",
    "    \"\"\"Create the data for the example.\"\"\"\n",
    "    data = {}\n",
    "    weights = [48, 30, 19, 36, 36, 27, 42, 42, 36, 24, 30]\n",
    "    data[\"weights\"] = weights\n",
    "    data[\"items\"] = list(range(len(weights)))\n",
    "    data[\"bins\"] = data[\"items\"]\n",
    "    data[\"bin_capacity\"] = 100\n",
    "    return data\n",
    "\n",
    "\n",
    "\n",
    "def main():\n",
    "    data = create_data_model()\n",
    "\n",
    "    # Create the mip solver with the SCIP backend.\n",
    "    solver = pywraplp.Solver.CreateSolver(\"SCIP\")\n",
    "\n",
    "    if not solver:\n",
    "        return\n",
    "\n",
    "    # Variables\n",
    "    # x[i, j] = 1 if item i is packed in bin j.\n",
    "    x = {}\n",
    "    for i in data[\"items\"]:\n",
    "        for j in data[\"bins\"]:\n",
    "            x[(i, j)] = solver.IntVar(0, 1, \"x_%i_%i\" % (i, j))\n",
    "\n",
    "    # y[j] = 1 if bin j is used.\n",
    "    y = {}\n",
    "    for j in data[\"bins\"]:\n",
    "        y[j] = solver.IntVar(0, 1, \"y[%i]\" % j)\n",
    "\n",
    "    # Constraints\n",
    "    # Each item must be in exactly one bin.\n",
    "    for i in data[\"items\"]:\n",
    "        solver.Add(sum(x[i, j] for j in data[\"bins\"]) == 1)\n",
    "\n",
    "    # The amount packed in each bin cannot exceed its capacity.\n",
    "    for j in data[\"bins\"]:\n",
    "        solver.Add(\n",
    "            sum(x[(i, j)] * data[\"weights\"][i] for i in data[\"items\"])\n",
    "            <= y[j] * data[\"bin_capacity\"]\n",
    "        )\n",
    "\n",
    "    # Objective: minimize the number of bins used.\n",
    "    solver.Minimize(solver.Sum([y[j] for j in data[\"bins\"]]))\n",
    "\n",
    "    print(f\"Solving with {solver.SolverVersion()}\")\n",
    "    status = solver.Solve()\n",
    "\n",
    "    if status == pywraplp.Solver.OPTIMAL:\n",
    "        num_bins = 0\n",
    "        for j in data[\"bins\"]:\n",
    "            if y[j].solution_value() == 1:\n",
    "                bin_items = []\n",
    "                bin_weight = 0\n",
    "                for i in data[\"items\"]:\n",
    "                    if x[i, j].solution_value() > 0:\n",
    "                        bin_items.append(i)\n",
    "                        bin_weight += data[\"weights\"][i]\n",
    "                if bin_items:\n",
    "                    num_bins += 1\n",
    "                    print(\"Bin number\", j)\n",
    "                    print(\"  Items packed:\", bin_items)\n",
    "                    print(\"  Total weight:\", bin_weight)\n",
    "                    print()\n",
    "        print()\n",
    "        print(\"Number of bins used:\", num_bins)\n",
    "        print(\"Time = \", solver.WallTime(), \" milliseconds\")\n",
    "    else:\n",
    "        print(\"The problem does not have an optimal solution.\")\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
